package com.fulihui.wgj.service.reply;

import com.alibaba.dubbo.config.annotation.Service;
import com.fulihui.common.builder.rpc.RpcResultBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.error.Errors;
import com.fulihui.common.error.InvokeServiceException;
import com.fulihui.common.request.rpc.AppRequest;
import com.fulihui.common.request.rpc.PageAppRequest;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.util.ConditionValidator;
import com.fulihui.wgj.dal.mybatis.domain.WxSubscribeReplyAdvert;
import com.fulihui.wgj.dal.mybatis.repository.WxSubscribeReplyAdvertRepository;
import com.fulihui.wgj.facade.dto.WxSubscribeReplyAdvertDTO;
import com.fulihui.wgj.facade.request.PrimaryKeyRequest;
import com.fulihui.wgj.facade.request.reply.WxSubscribeReplyAdvertSaveRequest;
import com.fulihui.wgj.facade.result.reply.WxSubsribeAdvertPageResult;
import com.fulihui.wgj.facade.service.reply.WxSubscribeAdverseService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 回复规则广告库Service
 *
 * @author wang_wx
 * @date 2018-07-30
 */
@Component
@Service(interfaceClass = WxSubscribeAdverseService.class, version = "1.0.0", timeout = 3000)
public class WxSubscribeAdverseServiceImpl implements WxSubscribeAdverseService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxSubscribeAdverseServiceImpl.class);

    @Autowired
    WxSubscribeReplyAdvertRepository wxSubscribeReplyAdvertRepository;

    @Override
    public SimpleResult<List<WxSubscribeReplyAdvertDTO>> queryAdvert(AppRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        WxSubscribeReplyAdvert wxSubscribeReplyAdvert = new WxSubscribeReplyAdvert();
        wxSubscribeReplyAdvert.setAppId(request.getAppId());
        List<WxSubscribeReplyAdvert> replyAdverts = wxSubscribeReplyAdvertRepository.queryByWhere(wxSubscribeReplyAdvert);
        List<WxSubscribeReplyAdvertDTO> advertDTOS = null;
        if (!CollectionUtils.isEmpty(replyAdverts)) {
            advertDTOS = BeanConv.copy(replyAdverts, WxSubscribeReplyAdvertDTO.class);
        }
        return RpcResultBuilder.succSimple(advertDTOS);
    }

    @Override
    public WxSubsribeAdvertPageResult pageQueryAdvert(PageAppRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        WxSubscribeReplyAdvert wxSubscribeReplyAdvert = new WxSubscribeReplyAdvert();
        wxSubscribeReplyAdvert.setAppId(request.getAppId());
        PageInfo<WxSubscribeReplyAdvert> pageInfo = wxSubscribeReplyAdvertRepository.queryPageListByWhere(wxSubscribeReplyAdvert, request.getPage(), request.getRows());
        long total = pageInfo.getTotal();
        WxSubsribeAdvertPageResult result = RpcResultBuilder.succPageResult(WxSubsribeAdvertPageResult.class, pageInfo.getPageNum(), pageInfo.getPageSize(), Math.toIntExact(total));
        if (total > 0) {
            result.setValue(BeanConv.copy(pageInfo.getList(), WxSubscribeReplyAdvertDTO.class));
        }
        return result;
    }

    @Override
    public SimpleResult<WxSubscribeReplyAdvertDTO> queryAdvert(PrimaryKeyRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        WxSubscribeReplyAdvert advert = wxSubscribeReplyAdvertRepository.queryById(request.getId());
        WxSubscribeReplyAdvertDTO advertDTO = null;
        if (Objects.nonNull(advert)) {
            advertDTO = BeanConv.copy(advert, WxSubscribeReplyAdvertDTO.class);
        }
        return RpcResultBuilder.succSimple(advertDTO);
    }

    @Override
    public BaseResult deleteReplyAdvertById(PrimaryKeyRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        Integer delete = wxSubscribeReplyAdvertRepository.deleteById(request.getId());
        return delete > 0 ? RpcResultBuilder.succ() : RpcResultBuilder.fail(Errors.Commons.DELETE_FAILED);
    }

    @Override
    public BaseResult saveReplyRule(WxSubscribeReplyAdvertSaveRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        WxSubscribeReplyAdvert advert = BeanConv.copy(request, WxSubscribeReplyAdvert.class);
        Integer id = advert.getId();
        Integer count;
        if (Objects.isNull(id)) {
            count = wxSubscribeReplyAdvertRepository.saveSelective(advert);
        } else {
            advert.setGmtModified(new Date());
            count = wxSubscribeReplyAdvertRepository.updateSelective(advert);
        }
        return count > 0 ? RpcResultBuilder.succ() : RpcResultBuilder.fail(Errors.Commons.SAVE_FAILED);
    }
}
